package com.czuft.usersystem.service;

import com.czuft.usersystem.component.PathComponent;
import com.czuft.usersystem.jpa.UserRepository;
import com.czuft.usersystem.mapper.UserMapper;
import com.czuft.usersystem.pojo.User;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

//    private List<Paper> paperList = new ArrayList<Paper>();
//    private List<Question> queslist = new ArrayList<Question>();

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRepository userRepository;

    @Resource
    PathComponent pathComponent;

    public Map<String, Object> login(User user) {

        Map<String, Object> backObject = new HashMap<>();
        User u = userMapper.login(user);
        if (u == null) {
            return backObject;
        }

        backObject.put("user", u);
        return backObject;
    }


    public boolean selectByName(User user)  {

        User u = userMapper.selectByName(user);
        return u == null;
    }

    public void deleteUserById(User user) {
        userMapper.deleteUserById(user);

    }

    public User getUserByName(User user) {
        return userMapper.selectByName(user);
    }


    public List<User> getUserlistByName(User pojo)  {
        List<User> userlist;
        if (pojo == null || pojo.getName() == null || pojo.getName().equals("")) {
            userlist = userMapper.getUserlistAll();
        } else {
            userlist = userMapper.getUserlistByName(pojo);
        }
        for (User tuser : userlist) {
            if (tuser.getUserimg() == null || tuser.getUserimg().equals("")) {
                tuser.setUserimg("/upload/user.png");
            }
        }

        return userlist;

    }

    public List<User> getUserlistAll()  {

        List<User> userlist = userMapper.getUserlistAll();
        for (User tuser : userlist) {
            if (tuser.getUserimg() == null || tuser.getUserimg().equals("")) {
                tuser.setUserimg("/upload/user.png");
            }
        }
        return userlist;
    }

    public User getUserById(long id) {
        // jpa 使用示例
        User user = userRepository.findById(id).orElse(null);
//        User user= userMapper.getUserById(id);
        if (user!=null && (user.getUserimg() == null || user.getUserimg().equals(""))) {
            user.setUserimg("/upload/user.png");
        }
        return user;
    }

    public User insert(User user) {
        // userMapper.insert(user);
        userRepository.save(user);
        user = userMapper.selectByName(user);
        if (user.getUserimg() == null || user.getUserimg().equals("")) {
            user.setUserimg("/upload/user.png");
        }
        return user;
    }

    public User update(User user) {
        userMapper.update(user);
        user = userMapper.selectByName(user);
        if (user.getUserimg() == null || user.getUserimg().equals("")) {
            user.setUserimg("/upload/user.png");
        }
        return user;
    }


    public boolean insertUser(User user) {

        try {
            userMapper.insert(user);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public User getPwdByPhoneOrMail(User user) {

        try {
            user = userMapper.getPwdByPhoneOrMail(user);
            if (user.getUserimg() == null || user.getUserimg().equals("")) {
                user.setUserimg("/upload/user.png");
            }
            return user;

        } catch (Exception e) {
            return null;
        }

    }

    public boolean setPwdByMail(User user) {

        try {
            userMapper.setPwdByMail(user);

        } catch (Exception e) {
            return false;
        }
        return true;

    }

    public long selectCount() {

        try {
            return userMapper.selectCount();
        } catch (Exception e) {
            return 0;
        }
    }


}
